package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.QuerySendDetailsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.QuerySendStatisticsRequest;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.QuerySendDetailsResponseConvert;
import org.jiuling.aliyun.sms.convert.QuerySendStatisticsResponseConvert;
import org.jiuling.aliyun.sms.enums.SmsSendRangeEnum;
import org.jiuling.aliyun.sms.enums.TemplateTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsAsynchronous;
import org.jiuling.aliyun.sms.factory.service.AliYunSendSmsQueryService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunSendSmsQueryVerify;
import org.jiuling.aliyun.sms.model.QuerySendDetailsResponseModel;
import org.jiuling.aliyun.sms.model.QuerySendStatisticsResponseModel;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.validation.ObjectVerify;

import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * 阿里云异步请求短信查询服务实现类
 * @author 紫灵
 * @date 2023年02月09日 22时57分38秒
 */
public class AliYunAsyncSendSmsQueryServiceImpl extends AliYunSmsAsynchronous implements AliYunSendSmsQueryService {

    public AliYunAsyncSendSmsQueryServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public QuerySendStatisticsResponseModel querySendStatistics(String signName,SmsSendRangeEnum smsSendRange, String startDate, String endDate, TemplateTypeEnum templateType, int pageNum, int pageSize) {
        // 校验查询短信统计信息参数
        AliYunSendSmsQueryVerify.querySendStatisticsParam(smsSendRange, startDate, endDate);
        // 获取模板类型
        Integer templateTypeV = null;
        if(!ObjectVerify.isNull(templateType)){
            templateTypeV = templateType.getValue();
        }
        // 构建查询短信统计信息请求
        QuerySendStatisticsRequest querySendStatisticsRequest = QuerySendStatisticsRequest.builder()
                .isGlobe(smsSendRange.getValue())
                .startDate(startDate)
                .endDate(endDate)
                .pageIndex(pageNum)
                .pageSize(pageSize)
                .templateType(templateTypeV)
                .signName(signName)
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();
        // 创建客户端
        try (AsyncClient asyncClient = create()){
            // 异步获取API请求的返回值
            return QuerySendStatisticsResponseConvert.bodyToModel(asyncClient.querySendStatistics(querySendStatisticsRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySendDetailsResponseModel querySendDetails(String phoneNumber, String bizId, Date sendDate,long pageNum,long pageSize) {
        // 校验查询发送详情参数
        AliYunSendSmsQueryVerify.querySendDetailsParam(phoneNumber, sendDate);
        // 构建查询发送详情请求
        QuerySendDetailsRequest querySendDetailsRequest = QuerySendDetailsRequest.builder()
                .phoneNumber(phoneNumber)
                .bizId(bizId)
                .sendDate(DateConvert.dateToString(sendDate, DateTimeFormatEnum.yyyyMMdd))
                .pageSize(pageSize)
                .currentPage(pageNum)
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();
        // 创建客户端
        try (AsyncClient asyncClient = create()){
            return QuerySendDetailsResponseConvert.bodyToModel(asyncClient.querySendDetails(querySendDetailsRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
